package com.utooo.service.supplier;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.constant.ResultCodeConstant;
import com.utooo.dao.purchase.OrderInvoiceAssociationMapper;
import com.utooo.dao.supplier.SupplierOrderInvoiceAssociationMapper;
import com.utooo.pojo.domain.purchase.order.OrderInvoiceAssociationDO;
import com.utooo.pojo.domain.supplier.settlement.SupplierOrderInvoiceAssociationDO;
import com.utooo.pojo.dto.common.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author new
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SupplierOrderInvoiceAssociationServiceImpl extends ServiceImpl<SupplierOrderInvoiceAssociationMapper, SupplierOrderInvoiceAssociationDO>
        implements SupplierOrderInvoiceAssociationService {

    private final OrderInvoiceAssociationMapper orderInvoiceAssociationMapper;


    @Override
    public RestResult<String> confirmOrderInvoiceAssociation(String id) {
        SupplierOrderInvoiceAssociationDO associationDO = this.getById(id);
        if (associationDO == null) {
            log.error("供应商订单关联发票记录为空！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "供应商订单关联发票记录为空！");
        }
        String purchaseOrderInvoiceId = associationDO.getPurchaseOrderInvoiceId();
        OrderInvoiceAssociationDO orderInvoiceAssociation = orderInvoiceAssociationMapper.selectById(purchaseOrderInvoiceId);
        if (orderInvoiceAssociation == null) {
            log.error("采购方订单关联发票记录为空！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购方订单关联发票记录为空！");
        }
        orderInvoiceAssociation.setStatus("2");
        orderInvoiceAssociationMapper.updateById(orderInvoiceAssociation);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @Override
    public RestResult<String> revokeOrderInvoiceAssociation(String id) {
        SupplierOrderInvoiceAssociationDO associationDO = this.getById(id);
        if (associationDO == null) {
            log.error("供应商订单关联发票记录为空！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "供应商订单关联发票记录为空！");
        }
        String purchaseOrderInvoiceId = associationDO.getPurchaseOrderInvoiceId();
        OrderInvoiceAssociationDO orderInvoiceAssociation = orderInvoiceAssociationMapper.selectById(purchaseOrderInvoiceId);
        if (orderInvoiceAssociation == null) {
            log.error("采购方订单关联发票记录为空！");
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购方订单关联发票记录为空！");
        }
        orderInvoiceAssociation.setStatus("3");
        orderInvoiceAssociationMapper.updateById(orderInvoiceAssociation);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }
}